<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>角色列表</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/css/oksub.css">
    <script type="text/javascript" src="/static/lib/loading/okLoading.js"></script>
</head>
<body>
<div class="ok-body">
    <!--模糊搜索区域-->
    <div class="layui-row">
        <form class="layui-form ok-search-form">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" style="width: 340px;" class="layui-input" placeholder="账号" autocomplete="off"
                               name="username">
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <button class="layui-btn" style="margin-left: 150px;" lay-submit="" lay-filter="search">
                            <i class="layui-icon">&#xe615;</i>
                        </button>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <!--数据表格-->
    <table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
</div>
<!--js逻辑-->
<script src="/static/lib/layui/layui.js"></script>
<script>
    layui.use(["element", "jquery", "table", "form", "laydate", "okLayer", "okUtils", "okMock"], function () {
        let table = layui.table;
        let form = layui.form;
        let laydate = layui.laydate;
        let okLayer = layui.okLayer;
        let okUtils = layui.okUtils;
        let okMock = layui.okMock;
        let $ = layui.jquery;

        okLoading.close($);

        laydate.render({elem: "#startTime", type: "datetime"});
        laydate.render({elem: "#endTime", type: "datetime"});

        let userTable = table.render({
            elem: '#tableId',
            url: '/trees/role/',
            limit: 20,
            height: 'full-80',
            page: true,
            toolbar: true,
            toolbar: "#toolbarTpl",
            size: "sm",
            cols: [[
                {type: "checkbox", fixed: "left"},
                {field: "id", title: "ID", width: 60, sort: true},
                {field: "role_name", title: "角色名称", width: 200},
                {field: "role_byte", title: "权限字符", width: 150},
                {field: "remark_describe", title: "备注", width: 200},
                {field: "create_time", title: "创建时间", width: 200},
                {field: "first_time", title: "更新时间", width: 200},
                {field: "now_status", title: "状态", width: 100, templet: "#statusTpl", fixed: "right"},
                {title: "操作", align: "center", width: 250, fixed: "right", templet: "#operationTpl"}
            ]],
            done: function (res, curr, count) {

            }
        });
        // 这个地方说了个寂寞
        form.on("submit(search)", function (data) {
            userTable.reload({
                where: data.field,
                page: {curr: 1}
            });
            return false;
        });

        table.on("toolbar(tableFilter)", function (obj) {
            switch (obj.event) {
                case "batchEnabled":
                    batchEnabled();
                    break;
                case "batchDisabled":
                    batchDisabled();
                    break;
                case "batchDel":
                    batchDel();
                    break;
                case "add":
                    add();
                    break;
            }
        });

        table.on("tool(tableFilter)", function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case "edit":
                    edit(data);
                    break;
                case "del":
                    del(data);
                    break;
            }
        });

        function add() {
            okLayer.open("添加职称", "/static/pages/role/role_insert.html", "90%", "90%", null, function () {
                userTable.reload();
            })
        }

        function edit(data) {
            okLayer.open("更新职称", "/static/pages/title/title_edit.html", "90%", "90%", function (layero, index) {
                let iframe = window['layui-layer-iframe' + index];
                iframe.child(data);
            }, function () {
                userTable.reload();
            })
        }

        function del(data) {
            okLayer.confirm("确定要删除吗？", function () {
                okUtils.ajax("/trees/title/" + data.id, "delete", data.field, true).done(function (response) {
                    okUtils.tableSuccessMsg(response.msg);
                }).fail(function (error) {
                    console.log(error)
                });
            })
        }
    })
</script>
<!-- 头工具栏模板 -->
<script type="text/html" id="toolbarTpl">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="add"><i class="layui-icon">&#xe608;</i></a>
            添加职称
        </button>
    </div>
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="operationTpl">
    <a href="javascript:" title="编辑" lay-event="edit"><button class="layui-btn layui-btn-xs">编辑</button></a>
    &nbsp;
    <a href="javascript:" title="数据权限" lay-event="edit"><button class="layui-btn layui-btn-xs">数据权限</button></a>
    &nbsp;
    <a href="javascript:" title="分配用户" lay-event="edit"><button class="layui-btn layui-btn-xs">分配用户</button></a>
    &nbsp;
    <a href="javascript:" title="删除" lay-event="del"><button class="layui-btn layui-btn-danger layui-btn-xs">删除</button></a>
</script>

<script type="text/html" id="statusTpl">
    {{#  if(d.now_status == 0){ }}
    <span class="layui-btn layui-btn-xs">启用</span>
    {{#  } else if(d.now_status == 1) { }}
    <span class="layui-btn layui-btn-danger layui-btn-xs">停用</span>
    {{#  } }}
</script>

<script type="text/html" id="roleTpl">
    {{#  if(d.role == 0){ }}
    <span class="layui-btn layui-btn-normal layui-btn-xs">超级会员</span>
    {{#  } else if(d.role == 1) { }}
    <span class="layui-btn layui-btn-warm layui-btn-xs">普通用户</span>
    {{#  } }}
</script>
</body>
</html>
